package com.ming.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.context.BaseContext;
import com.ming.constant.StatusConstant;
import com.ming.dto.DishDTO;
import com.ming.dto.DishPageQueryDTO;
import com.ming.entity.Dish;
import com.ming.vo.DishVO;
import com.ming.vo.DishVOWithFlavors;
import com.ming.entity.DishFlavor;
import com.ming.mapper.DishFlavorMapper;
import com.ming.mapper.DishMapper;
import com.ming.result.PageResult;
import com.ming.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品和口味
     * @param dishDTO
     */
    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 菜品状态默认为启用状态
        dish.setStatus(StatusConstant.ENABLE);

        // 保存菜品基本信息
        dishMapper.insert(dish);

        // 获取菜品ID
        Long dishId = dish.getId();

        // 保存口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> dishVOs = dishMapper.pageQuery(dishPageQueryDTO);
        PageInfo<DishVO> pageInfo = new PageInfo<>(dishVOs);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据ID查询菜品和口味
     * @param id
     * @return
     */
    public DishVOWithFlavors getByIdWithFlavors(Long id) {
        // 查询菜品基本信息（包含分类名称）
        DishVO dishVO = dishMapper.getById(id);

        // 查询菜品口味信息
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);

        // 封装到VO
        DishVOWithFlavors dishVOWithFlavors = new DishVOWithFlavors();
        BeanUtils.copyProperties(dishVO, dishVOWithFlavors);
        dishVOWithFlavors.setFlavors(flavors);

        return dishVOWithFlavors;
    }

    /**
     * 根据ID删除菜品
     * @param id
     */
    @Transactional
    public void deleteById(Long id) {
        // 删除菜品基本信息
        dishMapper.deleteById(id);

        // 删除菜品口味信息
        dishFlavorMapper.deleteByDishId(id);
    }

    /**
     * 根据ID批量删除菜品
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        // 判断当前菜品是否能够删除---是否存在起售中的菜品？？

        // TODO 判断当前菜品是否能够删除---是否被套餐关联了？？

        // 批量删除菜品基本信息
        dishMapper.deleteByIds(ids);

        // 批量删除菜品口味信息
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 修改菜品和口味
     * @param dishDTO
     */
    @Transactional
    public void updateWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 修改菜品基本信息
        dishMapper.update(dish);

        // 删除原有口味
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        // 重新插入口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 启用或禁用菜品
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类ID查询菜品
     * @param categoryId
     * @return
     */
    public List<DishVO> getByCategoryId(Long categoryId) {
        List<DishVO> dishVOs = dishMapper.getByCategoryId(categoryId);
        return dishVOs;
    }
}